package com.xiaojie.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaojie.entity.User;
import com.xiaojie.util.JwtUtil;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * ClassNameJWTLoginFilter
 *
 * @Description 登录过滤器
 * @Author yan
 * 2021/4/28 23:18
 * @Version 1.0
 **/
public class JWTLoginFilter  extends UsernamePasswordAuthenticationFilter {
    private AuthenticationManager authenticationManager;

    public JWTLoginFilter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
        /**
         *  后端登陆接口为登陆接口 放行接口
         */
        super.setFilterProcessesUrl("/login");
    }

    @Override
    /**
     * @Description: 登录
     * @Author: yan
     * @Date: 2021/4/28 23:32
     * @param request:
     * @param response:
     * @return: org.springframework.security.core.Authentication
     **/
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        //  调用我们userService   账号密码登陆
        try {
            User user = new ObjectMapper()
                    .readValue(request.getInputStream(), User.class);
             authenticationManager.authenticate(
                     new UsernamePasswordAuthenticationToken(
                             user.getUsername(),
                             user.getPassword(),
                             new ArrayList<>()
                     )
             );
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
return null;
    }

    @Override
    /**
     * @Description: 账号密码登录成功
     * @Author: yan
     * @Date: 2021/4/28 23:33
     * @param request:
     * @param response:
     * @param chain:
     * @param authResult:
     * @return: void
     **/
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
       //登录成功，生成jwt返回给客户端
        User user= (User) authResult.getPrincipal();
        String jsonWebToken = JwtUtil.generateJsonWebToken(user);
        response.addHeader("token",jsonWebToken);
    }

    @Override
    /**
     * @Description:账号密码验证失败
     * @Author: yan
     * @Date: 2021/4/28 23:33
     * @param null:
     * @return: null
     **/
    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
        super.unsuccessfulAuthentication(request, response, failed);
    }
}
